

import * as path from 'path';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as ts from 'typescript';
import { ClassDeclaration, SyntaxKind } from 'typescript';
import { findFiles } from './utils';
import { generateZod } from './utils/operate';

/**
 * 由dto生成zod
 * @param relativePath dto文件夹相对路径 (相对执行命令的路径)
 * @param fileFormatList 目标文件正则列表
 */
export default function dto2zod(
  relativePath: string = './src/types',
  fileFormatList: Array<RegExp> = [/^.*?.dto.ts$/, /^.*?.vo.ts$/],
) {
  const typesDir = path.resolve(process.cwd(), relativePath);
  findFiles(typesDir, fileFormatList).then(files=>{
    files.forEach(filename => {
      const content = fs.readFileSync(filename, 'utf8');
      const astTree = ts.createSourceFile('temp.ts', content, ts.ScriptTarget.ES2015);
      // 找到所有类定义
      astTree.statements
        .filter(item => item.kind === SyntaxKind.ClassDeclaration)
        .forEach(item => generateZod({
          filename: path.basename(filename),
          dto: <ClassDeclaration>item,
          originContent: content
        }))
    })
  })
}